## HEADER ####
## 2024-08-29
## THI data
## Last edited by: Ed H

# start_date <- '2014-01-01'
# end_date <- '2023-03-16'
# 

# https://www.pericoli.com/en/temperature-humidity-index-what-you-need-to-know-about-it/#:~:text=THI%20%3D0.8*T%20%2B%20RH,humidity%20is%20expressed%20as%200.75.
# THI =0.8*T + RH*(T-14.4) + 46.4

# Temp <- 22
# RH <- 5
# 0.8 * Temp + RH/100 * (Temp-14.4) + 46.4

# https://docs.ropensci.org/nasapower/
# documentation
# https://docs.ropensci.org/nasapower/articles/nasapower.html

library("nasapower")

# CERC weather station lonlat = c(-2.426, 52.777)

data <- get_power(
  community = "ag",
  lonlat = c(-2.426, 52.777),
  pars = c("T2M",  
           "PRECTOTCORR", "ALLSKY_SFC_PAR_TOT", 
           "GWETTOP", "GWETROOT", "CLOUD_AMT",
           "RH2M"),
  dates = c("2000-01-01", "2024-08-29"),
  temporal_api = "daily"
)

data$THI <- (0.8*data$T2M + data$RH2M/100*(data$T2M-14.4) + 46.4)

write.csv(data, file = "THI_2000_20240829.csv", row.names=F)

# rm(data)